//
//  XCWelcomViewController.swift
//  小陈微博Swift用纯代码创建
//
//  Created by 小陈 on 16/2/29.
//  Copyright © 2016年 小陈. All rights reserved.
//

import UIKit
import SDWebImage
import SVProgressHUD

class XCWelcomViewController: UIViewController {
    
    var IconIamgeViewButtomConsTraint : NSLayoutConstraint?
 
    override func viewDidLoad() {
        super.viewDidLoad()
        // 准备UI
        prepareUI()
        setUpIcon()
        // 加载用户数据
        XCUSerAccountViewModel.sharedInstance.loadUserInfo { (error) -> () in
            if error != nil {
        print("XCNetworkingTool: 加载用户数据成功\(error)")
                return
            }
            // 设置欢迎界面的图片
            self.setUpIcon()
        }

    }
    func setUpIcon(){
        if let IconView = XCUSerAccountViewModel.sharedInstance.userAccount?.avatar_large{

            let Url = NSURL(string: IconView)
            IconImage.sd_setImageWithURL(Url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
// 头像的动画
   MoveAnimation()
    }

    //MARK: - 准备UI界面
    func prepareUI() {
        // 添加控件
        view.addSubview(bgImageName)
        view.addSubview(IconImage)
        view.addSubview(MessageLable)
        
/*=====================添加约束的时候一定要取消 Autoresizing 的影响=======================*/
        IconImage.translatesAutoresizingMaskIntoConstraints = false
        MessageLable.translatesAutoresizingMaskIntoConstraints = false
        bgImageName.translatesAutoresizingMaskIntoConstraints = false


        // 背景的约束
        view.addConstraint(NSLayoutConstraint(item: bgImageName, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: bgImageName, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
      // 头像的约束
        view.addConstraint(NSLayoutConstraint(item: IconImage, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        // 定义头像的属性
        IconIamgeViewButtomConsTraint = NSLayoutConstraint(item: IconImage, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
        
        view.addConstraint(IconIamgeViewButtomConsTraint!)
        
view.addConstraint(NSLayoutConstraint(item: IconImage, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem:nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
     
        view.addConstraint(NSLayoutConstraint(item: IconImage, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem:nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        
        
        // Lable的约束
        view.addConstraint(NSLayoutConstraint(item: MessageLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: IconImage, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        view.addConstraint(NSLayoutConstraint(item: MessageLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: IconImage, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
    /*==================================================================================*/
    }
   //MARK: -  头像的动画
    func MoveAnimation(){
        IconIamgeViewButtomConsTraint?.constant = -(UIScreen.mainScreen().bounds.height - 160)
        UIView.animateWithDuration(1.25, delay: 0.125, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            //  绘画在布局之前执行的代码，跟新控件的frame值
      self.view.layoutIfNeeded()
            }) { (_) -> Void in
                
                // 显示主界面了
                let App =  UIApplication.sharedApplication().delegate as! AppDelegate
                App.window?.rootViewController = XCMainViewController()
        }
    }
    
    
    // MARK: - 懒加载属性
    /// 头像
    lazy var IconImage:UIImageView = {
        let Icon = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        Icon.layer.cornerRadius = 42.5
        
        Icon.clipsToBounds = true
        Icon.sizeToFit()
        
        return Icon
        }()
    /// 显示的文字
    lazy var MessageLable:UILabel = {
        let lable = UILabel()
        lable.text = "欢迎回来"
        lable.textColor = UIColor.blackColor()
        lable.font = UIFont.systemFontOfSize(14)
        lable.sizeToFit()
        return lable
        }()
    /// 背景图片
    lazy var bgImageName :UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    

   
}
